package com.wondertek.poms.sync.manager.main;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.wondertek.poms.common.constant.Constants;
import com.wondertek.poms.core.service.IPrdContService;
import com.wondertek.poms.core.service.IPrdInfoService;
import com.wondertek.poms.core.utils.OutHttpUtil;
import com.wondertek.poms.dao.po.Content;
import com.wondertek.poms.dao.po.ContentBlack;
import com.wondertek.poms.dao.po.ContentWhite;
import com.wondertek.poms.dao.po.PrdCont;
import com.wondertek.poms.dao.po.PrdInfo;
import com.wondertek.poms.dao.po.ProductInfoPackage;
import com.wondertek.poms.dao.po.SyncTask;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * <p><b>MainAssertManager Description:</b> 其他类型媒资业务类</p>
 *
 * @author douzi
 * <b>DATE</b> 2019年8月20日 下午5:28:39
 */
@Slf4j
@Component
public class MainAssertManager extends ParentManager {
	
    @Autowired
    IPrdInfoService prdInfoService;
    @Autowired
    IPrdContService prdContService;
    @Autowired
    OutHttpUtil outHttpUtil;

    /**
     * <p><b>Title:</b> mainProcess</p>
     * <p><b>Description:</b> 主业务流程</p>
     *
     * @param vo
     * @author douzi
     */
    public void mainProcess(Content c, SyncTask st, ProductInfoPackage pip) {
        switch (c.getStatus().intValue()) {
            case Constants.ASSET_STATUS_0:
            case Constants.ASSET_STATUS_1:
            case Constants.ASSET_STATUS_3:
                mainUpdateProcess(c, st, pip);
                break;
            case Constants.ASSET_STATUS_2:
                mainDelProcess(st, "主流程");
                break;
            default:
                log.info("主流程|媒资:" + st.getAssetId() + "|不属于任何一种媒资状态:" + c.getStatus());
                break;
        }
    }
    
	/**
     * <p><b>Title:</b> moveProcess</p>
     * <p><b>Description:</b> 移包主流程</p>
     *
     * @param newPrdInfo
     * @param vo
     * @author ma
     */
//    public void moveProcess(PrdInfo newPrdInfo, Content c, SyncTask st) {
//        if (newPrdInfo != null) {
//            log.info("移包主流程|AssetId:" + st.getAssetId() + "|ProductInfoPackageId:" + newPrdInfo.getProductInfoPackageId());
//            List<PrdCont> assetVos = prdContService.getPrdContByMmsId(st.getAssetId(), newPrdInfo.getProductInfoPackageId());
//        }
//    }

	/**
     * <p><b>Title:</b> subSetUpdateProcess</p>
     * <p><b>Description:</b> 媒资新增、更新流程 </p>
     *
     * @param vo
     * @author douzi
     */
    private void mainUpdateProcess(Content c, SyncTask st, ProductInfoPackage pip) {
        List<PrdInfo> prdInfos = getPrdInfo(pip.getProductInfoPackageId());
        prdInfos = filterBlack(c, pip, prdInfos);

        if (CollectionUtil.isEmpty(prdInfos)) {
            log.info("主流程|媒资ID:" + st.getAssetId() + "|内容:" + c.getContentId() + "|二级产品:" + pip.getProductInfoPackageId() + "下没有商品包");
            return;
        } else {
            log.info("主流程|媒资ID:" + st.getAssetId() + "|内容:" + c.getContentId() + "|入包二级产品:" + pip.getProductInfoPackageId() + "|包数量:" + prdInfos.size());
        }

//        Long newPrdInfoId = getMoveInPrdInfo(c, pip);
        Long newPrdInfoId = filterWhite(c, pip);
        
        if (newPrdInfoId == null) {
            newPrdInfoId = getWhichPrdInfo(c, prdInfos, pip);
        } else {
            newPrdInfoId = getPrdInfoIdExist(newPrdInfoId, prdInfos);
            //如果白名单中的商品包id属于黑名单中的包，需要走后续入包流程
            if (newPrdInfoId == null) {
                newPrdInfoId = getWhichPrdInfo(c, prdInfos, pip);
            }
        }

        List<PrdCont> prdContDbs = prdContService.getPrdContByMmsId(c.getAssetId(), pip.getProductInfoPackageId());
        log.info("主流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + "|入包二级产品:" + pip.getProductInfoPackageId() + "|能进入的包id:"
                + newPrdInfoId + "|数据库中媒资数量:" + prdContDbs.size() + "|版权id:" + c.getCopyrightObjectId());

        if (newPrdInfoId == null) {
    		offineContents(prdContDbs, prdInfos, c, "主流程");
            return;
        }

        PrdInfo prdInfo = getPrdInfo(prdInfos, newPrdInfoId);
        if (prdContDbs.size() <= 0) {
            inPrdInfo(prdInfo, c, null);
        } else if (prdContDbs.size() == 1) {
        	PrdCont pcDb = prdContDbs.get(0);
        	// 比较数据库中的内容是否和传入内容相同，不同需要走版权pk业务
        	if (pcDb.getContentId().equals(c.getContentId())) {
                inPrdInfo(prdInfo, c, pcDb);
        	} else {
        		findOptimalPrdCont(c, prdInfo, pcDb, "主流程");
        	}
        }

        //如果是壳，需要让子集跟着一起去新包
        subSetToNewPrdInfo(newPrdInfoId, prdContDbs, c);
    }

	/**
	 * <p><b>Title:</b> filterWhite</p>
	 * <p><b>Description:</b> 移入日志判断修改为白名单</p>
	 * @author douzi
	 * @param c
	 * @param pip
	 * @return
	 */
	private Long filterWhite(Content c, ProductInfoPackage pip) {
		ContentWhite params = new ContentWhite();
        params.setContentId(c.getContentId());
        params.setPackageId(pip.getProductInfoPackageId());
        List<ContentWhite> cws = contentWhiteService.findAll(params);
        log.info("主流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + "|入包二级产品:" + pip.getProductInfoPackageId() + "|白名单包数量:" + cws.size());
        if (cws == null || cws.size() <= 0) {
        	return null;
        }
        log.info("主流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + "|入包二级产品:" + pip.getProductInfoPackageId() + "|白名单包:" + JSONUtil.toJsonStr(cws));
        return cws.get(0).getPrdInfoId();
	}
	
	/**
	 * <p><b>Title:</b> filterBlack</p>
	 * <p><b>Description:</b> 过滤黑名单</p>
	 * @author douzi
	 * @param c
	 * @param pip
	 * @param prdInfos
	 * @return
	 */
	private List<PrdInfo> filterBlack(Content c, ProductInfoPackage pip, List<PrdInfo> prdInfos) {
		ContentBlack params = new ContentBlack();
        params.setContentId(c.getContentId());
        params.setPackageId(pip.getProductInfoPackageId());
        List<ContentBlack> cbs = contentBlackService.findAll(params);
        log.info("主流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + "|入包二级产品:" + pip.getProductInfoPackageId() + "|黑名单包数量:" + cbs.size());
        if (cbs == null || cbs.size() <= 0) {
        	return prdInfos;
        }
        
        StringBuffer sb = new StringBuffer();
        prdInfos = prdInfos.stream().filter(pi -> {
        	for (ContentBlack cb : cbs) {
        		if (cb.getPrdInfoId().equals(pi.getPrdInfoId())) {
        			sb.append(cb.getPrdInfoId() + ",");
        			return false;
        		}
        	}
        	return true;
        }).collect(Collectors.toList());
        log.info("主流程|媒资ID:" + c.getAssetId() + "|内容:" + c.getContentId() + "|入包二级产品:" + pip.getProductInfoPackageId() + "|黑名单包:" + JSONUtil.toJsonStr(sb));
		return prdInfos;
	}

	public void productInfoPackageDelProcess(ProductInfoPackage pip, SyncTask st) {
		List<PrdCont> prdContDbs = prdContService.findPcByContentId(st.getContentId(), pip.getProductInfoPackageId());
		offineContents(prdContDbs, "主流程");
	}
}
